package com.hubei.controller;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.core.JsonParser;
import com.google.gson.Gson;
import org.springframework.web.HttpRequestHandler;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class TreeController implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 解决中文乱码
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        System.out.println(name);
        // 返回json格式的数据
        Map<String, Object> map = new HashMap<>();
        map.put("name", "刘备");
        map.put("age", 30);
        // 把map这个java对象，转变成json字符串发送到客户端
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        Gson gson = new Gson();
        String result = gson.toJson(map);
        try {
            Thread.sleep(5000);
        } catch (Exception e){
            e.printStackTrace();
        }
        PrintWriter writer = response.getWriter();
        writer.write(result);
        writer.close();
    }
}
